package spike.jdbc.hello;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class MainInsertSafe {

	public static void main(String[] args) throws Exception {
		System.out.println("loading driver");
		Class.forName("org.hsqldb.jdbcDriver");

		System.out.println("connecting");
		Connection vConnection = DriverManager.getConnection(
				"jdbc:hsqldb:hsql://localhost/", "sa", "");

		String vSql = "INSERT INTO titre  (id, artiste, album, nom) values (?, ?, ?, ?)";
		PreparedStatement statement = vConnection.prepareStatement(vSql);

		// On simule des valeurs provenant d'une saisie utilisateur
		System.out.println("insert");
		statement.setInt(1, 4);
		statement.setString(2, "Rolling Stones");
		statement.setString(3, "Aftermath");
		statement.setString(4, "Paint it black");
		int vNbRecordTouched = statement.executeUpdate();

		System.out.println("inserted " + vNbRecordTouched + " records");

		statement.close();
		vConnection.close();

		System.out.println("done");
	}
}
